
#################################################################################################################
# Extension of the Analysis:
# --------------------------
# 
# Earlier, we analyzed the relationship between losses since peak and the likelihood of investors 
# topping up their current positions. Panel B of Figure 5 in the paper shows that the probability of 
# top-up increases as losses since peak price grow. This relationship is confirmed by regression 
# analysis, which shows a negative coefficient for the loss since peak variable.
# 
# This script computes Table A42. Table A42 extends this analysis by testing whether the negative slope 
# observed in Panel B of Figure 5 differs for cases where the investor did not hold the stock during 
# the peak price. However, the results in Table A42 show that the slopes are quantitatively similar 
# for peaks occurring before the purchase of the stocks and peaks occurring after the purchase.
# 
# Output File:
# ------------
# **Table A42: Top-Up Behavior When Stocks Are in Loss Since Past Peak Price (Placebo Test, Peaks Defined for the Past Year)**
# - `tables/clean_TOPUP2_returns_since_peak_past_year_peak_5update_loginsample.tex`
#################################################################################################################

{

  data_for_peaks[, female:=ifelse(gender=="F", 1, 0)]
  data_for_peaks[,port_value10000:=portfolio_value_all_stocks/10000]
  extreme.pv=data.table(sum_up(data_for_peaks[,.(port_value10000)], d=T))
  data_for_peaks[, outlier.pv:=0]
  
  data_for_peaks[port_value10000< extreme.pv$p1 | port_value10000> extreme.pv$p99, outlier.pv:=1]
  data_for_peaks[, distance_pur100:=distance_pur/100]
  data_for_peaks[, distance_peak:= as.numeric(port_date- date_past_point)]
  
  data_for_peaks[, distance_peak100:=distance_peak/100]
  data_for_peaks[, age10:=age/10]
  data_for_peaks[, Count_sedols10:=Count_sedols/10]
  
  library(lfe)
  library(stargazer)

  summary(mm1a<- felm(buy~ return.since.point_neg100    | 0 | 0 | anon + port_date, 
                     data_for_peaks[gain.since.point==0 & holding==0]))
  
  summary(mm1b<- felm(buy~ return.since.point_neg100  +   distance_peak100 +    distance_pur100+ port_value10000 +  Count_sedols10  +  account_tenure_years_to2016 +  female +  age10               | 0 | 0 | anon + port_date, data_for_peaks[gain.since.point==0 & outlier.pv==0 & holding==0]))
  
  summary(mm1c<- felm(buy~ return.since.point_neg100  +   distance_peak100 +  distance_pur100+ port_value10000 +  Count_sedols10                | anon  + Code_used_DS | 0 | anon + port_date, data_for_peaks[gain.since.point==0 & outlier.pv==0 & holding==0]))
  
  
  
  summary(mm2a<- felm(buy~ return.since.point_neg100    | 0 | 0 | anon + port_date, 
                      data_for_peaks[gain.since.point==0 & holding==1]))
  
  summary(mm2b<- felm(buy~ return.since.point_neg100  +   distance_peak100 +  distance_pur100+ port_value10000 +  Count_sedols10  +  account_tenure_years_to2016 +  female +  age10               | 0 | 0 | anon + port_date, data_for_peaks[gain.since.point==0 & outlier.pv==0 & holding==1]))
  
  summary(mm2c<- felm(buy~ return.since.point_neg100  +   distance_peak100 +   distance_pur100+ port_value10000 +  Count_sedols10                | anon  + Code_used_DS | 0 | anon + port_date, data_for_peaks[gain.since.point==0 & outlier.pv==0 & holding==1]))
  
  summary(mm2f<- felm(buy~ return.since.point_neg100*holding  +   distance_peak100 +  distance_pur100+ port_value10000 +  Count_sedols10                | anon  + Code_used_DS | 0 | anon + port_date, data_for_peaks[gain.since.point==0 & outlier.pv==0]))
  
  
  
  names_of_IV<-  unique(names(unlist(lapply(list(mm1a, mm1b, mm1c, mm2a, mm2b, mm2c, mm2f), coef))))[-1]
  
  
  
  important_labels<- c(
    "Return Since Peak $<0$ $(\\%)$",
    "Holding Stock During Past Peak=1",
    "Days Since Peak (100 days)",
    "Days Since Purchase (100 days)",
    "Portfolio Value (\\pounds10000)",
    "Number of Stocks (10 stocks)",
    "Account Tenure (years)",
    "Female=1",
    "Age (10 years)", 
    "Return Since Peak $<0$ $(\\%)$ $\\times$ Holding Stock During Past Peak=1")
  

  table<-stargazer( mm1a, mm1b, mm1c,mm2a, mm2b, mm2c, mm2f,
                    title="", align=TRUE,type="latex",
                    dep.var.labels=c(""),
                    covariate.labels=important_labels,
                    omit        = NULL ,  
                    omit.stat=c("LL","ser","f", "adj.rsq") , 
                    omit.table.layout = "n",
                    float=F, 
                    table.layout ="-dc#-tas-",
                    style = "aer",
                    column.sep.width = "10pt",
                    add.lines=list(c("Account FE", "NO", "NO",  "YES","NO", "NO",  "YES", "YES"),
                                   c("Stock FE",   "NO", "NO", "YES","NO", "NO",  "YES", "YES")), #
                    digits=4 #
  )

  library(stringr)
  table <- str_replace_all(table, "\\^", "")
  table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
  
  setwd(wd_tables)
  write.table(table[10:(length(table)-2)], col.names = F, 
              row.names = F, quote = FALSE, paste0("clean_TOPUP2_returns_since_peak_", plot_name, "_", use_days ,"sample.tex"))
  
}
